package cn.touna.jss.modules.custom.adress;

import cn.touna.jss.common.enums.IntfScopeEnum;
import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.credit.entity.CreditProvincialEntity;
import cn.touna.jss.modules.credit.entity.PhoneArea;
import cn.touna.jss.modules.credit.mapper.CarQueryHistoryMapper;
import cn.touna.jss.modules.credit.mapper.CreditProvinicalMapper;
import cn.touna.jss.modules.credit.mapper.PhoneAreaMapper;
import cn.touna.jss.modules.credit.service.Car300Province;
import cn.touna.jss.modules.custom.adress.entity.CarQueryHistory;
import cn.touna.jss.modules.custom.cheyiping.dto.MaintenanceRecordRequest;
import cn.touna.jss.modules.custom.cheyiping.mapper.CityEntityMapper;
import cn.touna.jss.modules.third.car300.entity.Car300PriceRequestEntity;
import cn.touna.risk.api.contraints.facet.First;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 内部数据服务组件
 * Created by wuwc on 2017/9/2.
 */
@Service
public class DataCenterService {

    private static final Logger logger = LogManager.getLogger(DataCenterService.class);



    @Resource
    private CreditProvinicalMapper provcitycountyMapper;


    @Autowired
    PhoneAreaMapper phoneAreaMapper;


    @Autowired
    Car300Province car300Province;


    @Autowired
    private CityEntityMapper cityEntityMapper;

    @Autowired
    CarQueryHistoryMapper carQueryHistoryMapper;

    /**
     * 身份证归属地查询
     *
     * @param requestEntity
     * @return
     */
    @ApiMapping(value = "InnerData.queryIdCardBelong",desc = "内部数据.身份证归属地查询",scope = IntfScopeEnum.JSS_INTF_SYSTEM)
    public Response queryIdCardBelong(CreditProvincialEntity requestEntity) {
        try {
            String idCardCode = requestEntity.getIdCard().substring(0, 6);
            CreditProvincialEntity provincialEntity = provcitycountyMapper.queryIdCardBelong(Integer.parseInt(idCardCode));
            if (provincialEntity == null) {
                return new Response().fail(SystemErrorCodeEnum.CRS_IDCARD_PROVINCE_NULL_FAIL);
            }
            JSONObject belongJson = new JSONObject();
            belongJson.put("provinceName", provincialEntity.getProvinceName());
            belongJson.put("cityName", provincialEntity.getCityName());
            belongJson.put("countyName", provincialEntity.getCountyName());
            belongJson.put("idCard", requestEntity.getIdCard());
            belongJson.put("year", provincialEntity.getYear());
//            return new Response().success(belongJson);  //存在code == null的风险
            return ResponseFactory.success(belongJson);
        } catch (Exception e) {
        }
        return null;
    }


    /**
     * 手机归属地查询
     *
     * @param request
     * @return
     */
    @ApiMapping(value = "InnerData.queryPhoneArea",desc = "内部数据.手机归属地查询",scope = IntfScopeEnum.JSS_INTF_SYSTEM)
    public Response queryPhoneArea(PhoneArea request) {
        try {
            Response validResponse = ValidUtil.valid(request, First.class);
            if (validResponse != null)
                return validResponse;

            PhoneArea phoneArea = phoneAreaMapper.queryPhoneArea(request);
            return new Response().success(SystemErrorCodeEnum.SYSTEM_REQUEST_SUCC, phoneArea);
        } catch (Exception e) {
            logger.error("PRO 调用【手机归属地查询-queryPhoneArea】异常,params:{}，msg:{}", request, e.getMessage());
            return ResponseFactory.fail(SystemErrorCodeEnum.CRS_SYSTEM_FAILED, e.getMessage());
        }
    }



    /**
     * 获取省份和简称
     *
     * @return
     * @throws Exception
     */
    @ApiMapping(value = "InnerData.getCarProvince",desc = "内部数据.获取省份和简称",scope = IntfScopeEnum.JSS_INTF_SYSTEM)
    public Response getCarProvince(Car300PriceRequestEntity request){
        List<Map<String,Object>> maps =    car300Province.getProvinces();
        return ResponseFactory.success(maps);
    }


    /**
     * 根据车牌号获取城市省份信息
     * @param request
     * @return
     */
    @ApiMapping(value = "InnerData.getCarCityInfo",desc = "内部数据.获取城市省份",scope = IntfScopeEnum.JSS_INTF_SYSTEM)
    public Response getCityInfo(MaintenanceRecordRequest request){
        Response response = ValidUtil.valid(request, First.class);
        if(response != null){
            return response;
        }
        Map<String, Object> cityEntity= cityEntityMapper.selectByCarCode(request.getCarNo());
        if(cityEntity ==null || cityEntity.size()<1){
            CarQueryHistory record =new CarQueryHistory();
            record.setCarCode(request.getCarNo());
            carQueryHistoryMapper.insert(record);
        }
        return ResponseFactory.success(cityEntity);

    }
}
